package com.htt.app.blogradio.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.htt.app.blogradio.BlogRadio;
import com.htt.app.blogradio.R;
import com.htt.app.blogradio.adapter.ListCustomAdapter;
import com.htt.app.blogradio.config.Custom;

public class CustomView implements OnClickListener{
	public boolean isCreate = false;
	
	private ListView list;
	private Activity activity;
	
	private ImageView btnHome;
	private TextView tvTitle;
	private ImageView btnBack;
	
	private View view;
	
	public View createView(Activity context) {
		// TODO Auto-generated constructor stub
		activity = context;
		view = LayoutInflater.from(context).inflate(R.layout.list, null);
		
		list = (ListView) view.findViewById(R.id.list);
		
		btnHome = (ImageView) view.findViewById(R.id.list_header_refresh);
		tvTitle = (TextView) view.findViewById(R.id.list_header_title);
		btnBack = (ImageView) view.findViewById(R.id.list_header_back);
//		btnBack.setVisibility(View.VISIBLE);
		
		btnHome.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		
		setData();
		isCreate = true;
		
		return view;
	}
	
	/*
	 *  set data for display
	 */
	public void setData() {
		tvTitle.setText(R.string.title_custom);
		
		List<com.htt.app.blogradio.object.Custom> data = new ArrayList<com.htt.app.blogradio.object.Custom>();
		int size = Custom.icon.length > Custom.name(activity).length ? Custom.name(activity).length : Custom.icon.length;
		
		for(int i = 0; i < size ; i++) {
			com.htt.app.blogradio.object.Custom custom = new com.htt.app.blogradio.object.Custom();
			custom.setName(Custom.name(activity)[i]);
			custom.setResource(Custom.icon[i]);
			data.add(custom);
		}
		
		ListCustomAdapter adapter = new ListCustomAdapter(activity, R.layout.list_custom_item, data);
		list.setAdapter(adapter);
	}
	
	public View getView(){
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.list_header_refresh:
			home();
			
			break;
			
		case R.id.list_header_back:
			home();
			break;
		
		default:
			break;
		}
	}
	
	private void home() {
		BlogRadio.home_view.removeAllViews();
		if(BlogRadio.playerView.isCreate)
			BlogRadio.home_view.addView(BlogRadio.playerView.getView());
		else
			BlogRadio.home_view.addView(BlogRadio.playerView.createView(activity,null));
		
		BlogRadio.changeStatusViewDisplay(BlogRadio.VIEWPLAYER);
	}
}
